#Alexander F. Gazmararian
#afg2@princeton.edu
#January 9, 2024

#Purpose: Load net-generation data to help identify shale shock timing

#Load packages
library(tidyverse)
library(tidylog)
library(here)

#load data
g <- read.csv(here("data", "input", "eia_monthlyenergyreview", "MER_T07_02A.csv"))
#drop last two column since all in the same unit
g <- subset(g, select = -c(Unit))
#fix names of fuel source
g$Description <- gsub("Electricity Net Generation From |, All Sectors", "", g$Description)
g$Description <- trimws(g$Description)
g$Description <- ifelse(g$Description=="Electricity Net Generation Total (including from sources not shown)", "Total", g$Description)
g$Description <- gsub(" ", "", g$Description)
table(g$Description)
#create year and month columns
g$month <- substr(g$YYYYMM, 5, 6)
g$year <- substr(g$YYYYMM, 0, 4)
#months that are 13 are the years before the monthly reporting was available
#aggregate months to years
agg <- g %>%
  group_by(year,Description) %>%
  summarise(netgen = sum(as.numeric(Value),na.rm=TRUE))
#pivot wider
aggwide <- pivot_wider(agg, names_from = Description, values_from = netgen, names_prefix = "netgen_")
#convert year to numeric
aggwide$year <- as.numeric(aggwide$year)
#save output
saveRDS(aggwide, here("data", "input", "eia_monthlyenergyreview", "eia_mer.rds"))
